<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\Admin;

class IfNode
{
	public function handle(Request $request, Closure $next)
	{
		if(session('fyback_info')['id'] != 1){
			$node = session('accessRights');
			if ($node === null) {
				$request->session()->flush();
				return redirect()->action('Admin\LoginController@index')->withErrors(['检测到您的权限状态异常,请重新登录']);
			}else{
				$route = Route::current()->getActionName();  //获取路由
				$action = explode('@',$route)[1];
				$controller = explode("\\", explode('@',$route)[0])[count(explode("\\", explode('@',$route)[0]))-1];
				$num = 0;
				foreach ($node as $key => $val) {
					if ($key == $controller && in_array($action, $val)) {
						++$num;
						break;
					}
				}
				if ($num>0) {
					return $next($request);
				} else {
					return redirect()->back()->withInput()->withErrors(['对不起,您暂无权限访问!']);
				}
			}
		}	
		return $next($request);
	}
}